#include "main.h"
#include "color.h"
#include "client.h"
pthread_t tid1 = {0};
pthread_t tid2 = {0};
void *everybod_talk_rcv(void *arg)//群聊接受消息
{
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    int fd = *(int *)arg;
    Msg data;
    while (1)
    {
        if (read(fd, &data, sizeof(data)))
        {
            printf("\n(群聊)->用户:%s\n", data.word);
        }
    }
    pthread_exit(NULL);
}
void *solo_talk_rcv(void *arg)//单聊接受消息
{
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    char buff[150] = {0};
    int jet = open("./2.txt", O_RDWR);
    int fd = *(int *)arg;
    Msg data;
    while (1)
    {
        if (read(fd, &data, sizeof(Msg)))
        {

            if (strcmp(data.word, "exit") == 0)
            {
                printf("对方已下线！\n");
                break;
            }
            else
            {
                sprintf(buff, "\n(单聊)->用户%s:%s", data.user_name, data.word);
                printf("%s\n", buff);
                lseek(jet, 0, SEEK_END);
                write(jet, buff, strlen(buff));
            }
        }
    }
    close(jet);
    pthread_exit(NULL);
}
void client_menu()//聊天界面选择
{
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_YELLOW "\t♥♥♥♥♥♥♥♥♥♥♥" BLINK "\t 开心聊天室   " DEFAULT_MODE FONT_HIGHLIGHT_YELLOW "♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_RED "\t♥♥♥♥♥♥♥♥♥♥♥       1.单聊      ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_BLUE "\t♥♥♥♥♥♥♥♥♥♥♥       2.群聊      ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_GREEN "\t♥♥♥♥♥♥♥♥♥♥♥       0.退出      ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
}
void client_MAIN_menu()//客户端界面
{
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_YELLOW "\t♥♥♥♥♥♥♥♥♥♥♥   " BLINK " WECHAT SYSTEM" DEFAULT_MODE FONT_HIGHLIGHT_YELLOW "  ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_RED "\t♥♥♥♥♥♥♥♥♥♥♥     1.注册账户    ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_BLUE "\t♥♥♥♥♥♥♥♥♥♥♥     2.登录账户    ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_GREEN "\t♥♥♥♥♥♥♥♥♥♥♥     0.退出系统    ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
}
void main_menu(int fd)//主菜单选择
{

    while (1)
    {
        client_MAIN_menu();
        printf(FONT_HIGHLIGHT_PURPLE "请选择您的操作：" DEFAULT_MODE);
        int opt;
        scanf("%d", &opt);
        system("clear");
        switch (opt)
        {
        case 1:
            user_reg(fd);
            break;
        case 2:
            if (user_login(fd) > 0)
            {
                client_choose(fd);
            }
            break;
        //.....
        case 0:
            printf(FONT_PURPLE "\t正在退出系统..." DEFAULT_MODE "\n");
            sleep(2);
            return;
        }
    }
}
bool user_reg(int fd)//注册账户
{
    Msg data;
    int ret = 0;
    printf("请输入用户账户:");
    scanf("%s", data.user_name);
    printf("请输入用户密码:");
    scanf("%s", data.user_pwd);
    data.type = 1;
    write(fd, &data, sizeof(data));
    read(fd, &ret, sizeof(ret));
    if (ret == 1)
    {
        printf("注册成功！\n");
        return true;
    }
    else
    {
        printf("注册失败！\n");
        return false;
    }
}
bool user_login(int fd)//登录账户
{
    Msg data;
    int ret = 0;
    printf("请输入用户账户:");
    scanf("%s", data.user_name);
    printf("请输入用户密码:");
    scanf("%s", data.user_pwd);
    data.type = 2;
    write(fd, &data, sizeof(data));
    read(fd, &ret, sizeof(ret));
    if (ret == 1)
    {
        printf("登录成功！\n");

        return true;
    }
    else
    {
        printf("登录失败！\n");
        return false;
    }
}
void client_choose(int fd)//聊天界面选择
{
    int input = 0;
    do
    {
        client_menu();
        printf(FONT_HIGHLIGHT_PURPLE "请选择您的操作：" DEFAULT_MODE);
        scanf("%d", &input);
        system("clear");
        while (getchar() != '\n')
            ;
        switch (input)
        {
        case 1:
            pthread_create(&tid1, NULL, solo_talk_rcv, &fd);
            pthread_detach(tid1);
            solo_talk_send(fd);
            pthread_cancel(tid1);
            break;
        case 2:

            pthread_create(&tid2, NULL, everybod_talk_rcv, &fd);
            pthread_detach(tid2);
            everybod_talk_send(fd);
            pthread_cancel(tid2);
            break;
        case 0:
            printf(FONT_PURPLE "\t正在退出系统..." DEFAULT_MODE "\n");
            sleep(2);
            break;
        default:
            printf(FONT_HIGHLIGHT_RED "\t输入错误！请重新输入：\n" DEFAULT_MODE);
            break;
        }
    } while (input);
}
void everybod_talk_send(int fd)//群聊发送消息
{
    Msg data;
    while (1)
    {
        printf("请输入要发送的内容:");
        scanf("%s", data.word);
        data.type = 4;
        if (strncmp(data.word, "exit", 4) == 0)
        {
            write(fd, &data, sizeof(data));

            return;
        }
        write(fd, &data, sizeof(data));
    }
}
void get_online_id(int fd)//获得在线玩家ID
{
    Msg data;
    client_msg client_save[10];
    data.type = 3;
    write(fd, &data, sizeof(Msg));
    char buff[10] = {0};
    read(fd, client_save, sizeof(client_save));
    for (int i = 0; i < 5; i++)
    {
        if (strlen(client_save[i].client_name) != 0)
        {
            printf("玩家%s在线\n", client_save[i].client_name);
        }
    }
}
void solo_talk_send(int fd)//单聊发送消息
{
    // get_online_id(fd);
    Msg data;
    printf("请输入私聊对象的用户名:");
    scanf("%s", data.user_name);
    while (1)
    {
        printf("请输入要发送的内容:");
        scanf("%s", data.word);
        data.type = 3;
        if (strncmp(data.word, "exit", 4) == 0)
        {
            write(fd, &data, sizeof(data));
            break;
        }
        else
            write(fd, &data, sizeof(Msg));
    }
}
int main()
{
    int clifd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(SER_PORT);
    addr.sin_addr.s_addr = inet_addr(SER_IP);
    int ret = connect(clifd, (struct sockaddr *)&addr, sizeof(addr));
    if (ret == -1)
    {
        printf("connect failed\n");
        return -1;
    }
    main_menu(clifd);
    close(clifd);
    return 0;
}